package com.mindskip.xzs.controller.student;

import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.context.WebContext;
import com.mindskip.xzs.domain.dto.student.message.MessagePageStudentDTO;
import com.mindskip.xzs.domain.dto.student.user.UserRegisterDTO;
import com.mindskip.xzs.domain.dto.student.user.UserUpdateStudentDTO;
import com.mindskip.xzs.domain.vo.admin.user.UserVO;
import com.mindskip.xzs.domain.vo.student.message.MessagePageStudentVO;
import com.mindskip.xzs.domain.vo.student.user.UserEventLogVO;
import com.mindskip.xzs.service.MessageUserService;
import com.mindskip.xzs.service.UserService;
import com.mindskip.xzs.utility.RestResponse;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController("StudentUserController")
@RequestMapping(value = "/api/student/user")
public class UserController {

    private final UserService userService;

    private final WebContext webContext;

    private final MessageUserService messageUserService;

    public UserController(UserService userService, WebContext webContext, MessageUserService messageUserService) {
        this.userService = userService;
        this.webContext = webContext;
        this.messageUserService = messageUserService;
    }

    @PostMapping("/register")
    public RestResponse<Object> register(@RequestBody @Valid UserRegisterDTO userRegisterDTO) {

        userService.studentRegister(userRegisterDTO);
        return RestResponse.ok();
    }

    @PostMapping("/log")
    public RestResponse<List<UserEventLogVO>> log() {

        List<UserEventLogVO> log = userService.log(webContext.getCurrentUser());
        return RestResponse.ok(log);
    }

    @PostMapping("/current")
    public RestResponse<UserVO> current() {

        UserVO userVO = UserVO.from(webContext.getCurrentUser());
        return RestResponse.ok(userVO);
    }

    @PostMapping("/update")
    public RestResponse<Object> update(@RequestBody @Valid UserUpdateStudentDTO userUpdateStudentDTO) {

        userService.updateUser(userUpdateStudentDTO, webContext.getCurrentUser());
        return RestResponse.ok();
    }

    @PostMapping("/message/page")
    public RestResponse<PageInfo<MessagePageStudentVO>> messagePage(@RequestBody @Valid MessagePageStudentDTO messagePageStudentDTO) {

        messagePageStudentDTO.setReceiveUserId(webContext.getCurrentUser().getId());
        PageInfo<MessagePageStudentVO> pageInfo = messageUserService.getMessageUserByPageAndReceiveUserId(messagePageStudentDTO);
        return RestResponse.ok(pageInfo);
    }

    // 消息标记已读
    @PostMapping("/message/read/{id}")
    public RestResponse<Object> read(@PathVariable Integer id) {

        messageUserService.readMessage(id);
        return RestResponse.ok();
    }

    // 未读消息数量
    @PostMapping("/message/unreadCount")
    public RestResponse<Integer> unreadCount() {

        Integer count = messageUserService.getUnreadCount(webContext.getCurrentUser());
        return RestResponse.ok(count);
    }
}
